const noble = require('noble');

// 扫描并寻找目标设备
noble.on('discover', function(peripheral) {
    // 找到目标设备后，连接设备
    if (peripheral.id === '00:11:22:33:44:55') {
        noble.stopScanning();
        console.log('Found device: ' + peripheral.advertisement.localName);
        console.log('Connecting to device...');
        peripheral.connect(function(error) {
            console.log('Connected to peripheral: ' + peripheral.uuid);
            // 在连接后，搜索服务和特征值
            peripheral.discoverServices([], function(error, services) {
                console.log('Found services: ' + services);
                services.forEach(function(service) {
                    service.discoverCharacteristics([], function(error, characteristics) {
                        console.log('Found characteristics: ' + characteristics);
                        characteristics.forEach(function(characteristic) {
                            console.log('Found characteristic: ' + characteristic.uuid);
                        });
                    });
                });
            });
        });
    }
});

// 扫描并搜索附近的蓝牙设备
noble.startScanning();
console.log('Scanning for devices...');